package cn.edu.scau.cmi.yangmupei;

import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.yangmupei.hibernate.domain.Student;
import cn.edu.scau.cmi.yangmupei.hibernate.domain.Teacher;
import cn.edu.scau.cmi.yangmupei.spring.factory.HibernateSessionFactory;
import cn.edu.scau.cmi.yangmupei.spring.service.StudentService;
import cn.edu.scau.cmi.yangmupei.spring.service.TeacherService;


public class SpringHibernateSessionClient {

	static Session session = HibernateSessionFactory.getSession();
	static Transaction transaction = null;
	static StudentService studentService = new StudentService(session);
	static TeacherService teacherService = new TeacherService(session);
	
	public static void main(String[] args) {
		System.out.println("欢迎来到此系统！");
		System.out.println("0、退出系统");
		System.out.println("1、添加学生");
		System.out.println("2、修改学生");
		System.out.println("3、删除学生");
		System.out.println("4、添加教师");
		System.out.println("5、修改教师");
		System.out.println("6、删除教师");
		System.out.println("7、学生选择指导教师");
		System.out.println("8、学生修改指导教师");
		System.out.println("9、教师选择被指导的学生");
		System.out.println("10、教师修改被指导的学生");
		System.out.print("请选择您想要的服务：");
		Scanner scanner = new Scanner(System.in);
		int key = scanner.nextInt();
		while(true) {
			switch (key) {
			case 1:
				System.out.print("请输入学生的学号：");
				Long number = scanner.nextLong();
				System.out.print("请输入学生的姓名：");
				String name = scanner.next();
				Student student = new Student();
				student.setName(name);
				student.setNumber(number);
				studentService.saveStudent(student);
				break;
			case 2:
				System.out.print("若修改姓名，请输入1，若修改学号，请输入2：");
				int select = scanner.nextInt();
				if(select==1) {
					System.out.print("请输入您要修改的学生的id：");
					Long id = scanner.nextLong();
					System.out.print("请输入您要修改的学生的姓名：");
					name = scanner.next();
					Student student2 = new Student();
					student2.setId(id);
					student2.setName(name);
					studentService.updateStudentName(student2);
				}
				else if(select == 2) {
					System.out.print("请输入您要修改的学生的id：");
					Long id = scanner.nextLong();
					System.out.print("请输入您要修改的学生的学号：");
					number = scanner.nextLong();
					Student student2 = new Student();
					student2.setId(id);
					student2.setNumber(number);
					studentService.updateStudentName(student2);
				}
				else {
					System.out.println("输入无效！");
				}
				break;
			case 3:
				System.out.print("请输入您要删除的学生的id：");
				Long id = scanner.nextLong();
				Student student3 = new Student();
				student3.setId(id);
				studentService.deleteStudent(student3);
				break;
			case 4:
				System.out.print("请输入教师的姓名：");
				name = scanner.next();
				Teacher tutor = new Teacher();
				tutor.setName(name);
				teacherService.saveTeacher(tutor);
				break;
			case 5:
				System.out.print("请输入您要修改的教师的id：");
				id = scanner.nextLong();
				System.out.print("请输入您要修改的教师的姓名：");
				name = scanner.next();
				Teacher tutor1 = new Teacher();
				tutor1.setId(id);
				tutor1.setName(name);
				teacherService.updateTeacher(tutor1);
				break;
			case 6:
				System.out.print("请输入您要删除的教师的id：");
				id = scanner.nextLong();
				Teacher tutor2 = new Teacher();
				tutor2.setId(id);
				teacherService.deleteTeacher(tutor2);
				break;
			default:
				if(key==7 || key ==8) {
					System.out.print("请输入学生的id：");
					id = scanner.nextLong();
					Student student2 = new Student();
					student2.setId(id);
					System.out.print("请输入教师的id：");
					id = scanner.nextLong();
					Teacher tutor3 = new Teacher();
					tutor3.setId(id);
					studentService.select(student2,tutor3,key);
				}else if(key == 9 || key == 10) {
					System.out.print("请输入教师的id：");
					id = scanner.nextLong();
					Teacher tutor3 = new Teacher();
					tutor3.setId(id);
					System.out.print("请输入学生的id：");
					id = scanner.nextLong();
					Student student2 = new Student();
					student2.setId(id);
					teacherService.select(student2, tutor3, key);
				}
			}
			if(key == 0) {
				System.out.println("本次服务到此结束！");
				break;
			}
			System.out.print("请继续选择您的服务：");
			key = scanner.nextInt();
		}
		session.close();

	}
}
